﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using BLL;
using Model;

namespace Web
{
    public partial class AdminManager_UI : Form
    {
        public AdminManager_UI()
        {
            InitializeComponent();
        }
        public Admin_BLL Admin_BLL = new Admin_BLL();
        Com autocoumns = new Com();
        private void AdminManager_UI_Load(object sender, EventArgs e)
        {
            #region DataGridView1绑定
            //需要添加列的列标题字符串
            string arraysHeaderText = @"管理员编号,密码,管理员类型,备注";
            //需要绑定数据库列名称的字符串
            string arraysName = @"LoginId,LoginPwd,LoginType,LoginRemark";


            //自动生成columns
            autocoumns.AutoColumn(arraysHeaderText, arraysName, dataGridView1);
            dataGridView1.DataSource = Admin_BLL.selectAdmin().Tables[0];
            autocoumns.AddColumn("修改", dataGridView1);
            autocoumns.AddColumn("删除", dataGridView1);

            //DataGridView1数据集绑定
            this.dataGridView1.DataSource = Admin_BLL.selectAdmin().Tables[0];
            //窗体加载时默认隐藏的列
            this.dataGridView1.Columns[1].Visible = false;
            #endregion
            comboBox1.SelectedIndex = 0;
        }
        public AdminManagerAdd_UI admadd;
        public void button1_Click(object sender, EventArgs e)
        {
            admadd = new AdminManagerAdd_UI();
            admadd.manager = this;
            admadd.ShowDialog();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public void btnSelect_Click(object sender, EventArgs e)
        {
            string txt = textBox1.Text;
            if (comboBox1.SelectedIndex == 0)
            {
                dataGridView1.DataSource = Admin_BLL.selectAdmin1(txt).Tables[0];
            }
            else
            {
                string box = comboBox1.SelectedItem.ToString();
                dataGridView1.DataSource = Admin_BLL.selectAdmin2(box,txt).Tables[0];
            }
        }

        public string loginid;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //选中行的图书编号
                loginid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

            }
            catch (Exception) { }
            int rows = dataGridView1.Columns.Count;
            if (e.ColumnIndex == rows - 2)//修改
            {
                AdminManagerExit_UI b = new AdminManagerExit_UI();
                b.manager = this;
                b.loginid = loginid;
                b.ShowDialog();
            }
            else if (e.ColumnIndex == rows - 1)//删除
            {
                DialogResult result = MessageBox.Show("确定删除吗？", "提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    if (Admin_BLL.deleteAdmin(loginid) > 0)
                    {
                        //调用查询按钮刷新图书信息表
                        btnSelect_Click(null, null);
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                    }
                }
            }
        }

        private void btnAddAdmInfo_Click(object sender, EventArgs e)
        {
            admadd = new AdminManagerAdd_UI();
            admadd.manager = this;
            admadd.ShowDialog();
        }
    }
}
